package com.funo.wcity.app.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.funo.wcity.app.service.mail.ReceiveMailService;
import com.funo.wcity.app.service.mail.receive.ReceiveMail;
import com.funo.wcity.app.service.mail.send.MailConstant;
import com.funo.wcity.app.utils.Base64Util;
import com.funo.wcity.app.utils.StringUtil;

@SuppressWarnings("all")
@Service("syncMail")
public class SyncMail implements Runnable {
	@Resource(name = "tblUsersService")
	private TblUsersService tblUsersService;
	@Resource(name = "receiveMailService")
	private ReceiveMailService receiveMailService;
	private static Log log = LogFactory.getLog(SyncMail.class); // 日志
	private String  path;
	private Integer idUser;
	private String  contextPath;

	/**
	 * 初始值
	 */
	public void init(String path, Integer idUser,String contextPath) {
		this.path = path;
		this.idUser = idUser;
		this.contextPath=contextPath;
	}

	public void run() {
		// 同步收件箱，垃圾箱邮件
		syncData();
	}

	/**
	 * 同步收件箱，垃圾箱邮件
	 */
	public synchronized void syncData() {
		try {
			List usersList = this.tblUsersService.getTblUsersList(idUser, null);
			for (int i = 0; i < usersList.size(); i++) {
				Map<String,String> objMap = (Map) usersList.get(i);
				System.out.println("==================解密值："+new String(Base64Util.decode(objMap.get("passWord"))));
				objMap.put("passWord",new String(Base64Util.decode(objMap.get("passWord"))));
				try{
					String email=String.valueOf(objMap.get("email"));
					String passWord=String.valueOf(objMap.get("passWord"));
					String imap=String.valueOf(objMap.get("imap"));
					String smtp=String.valueOf(objMap.get("smtp"));
					String pop=String.valueOf(objMap.get("pop"));
					Integer idUser=Integer.valueOf(String.valueOf(objMap.get("idUser")));
					ReceiveMail receiveMail = new ReceiveMail(email,passWord,smtp,imap,null);
					if(!StringUtil.isEmpty(imap) && !StringUtil.isEmpty(pop)){
						//imap接收邮件
						// 获取服务端收件箱邮件
						this.receiveMailService.getAnalMail(MailConstant.INBOX,receiveMail,idUser,path,contextPath,1);
						//获取服务端垃圾箱邮件
						this.receiveMailService.getAnalMail(MailConstant.SPAM, receiveMail,idUser,path,contextPath,1);
						//pop接收邮件
						// 获取服务端收件箱邮件
						this.receiveMailService.getAnalMail(MailConstant.INBOX, receiveMail,idUser,path,contextPath,2);
					}else if(!StringUtil.isEmpty(pop)){
						//pop接收邮件
						// 获取服务端收件箱邮件
						this.receiveMailService.getAnalMail(MailConstant.INBOX,receiveMail,idUser,path,contextPath,2);
					}else if(!StringUtil.isEmpty(imap)){
						//imap接收邮件
						// 获取服务端收件箱邮件
						this.receiveMailService.getAnalMail(MailConstant.INBOX,receiveMail,idUser,path,contextPath,1);
						//获取服务端垃圾箱邮件
						this.receiveMailService.getAnalMail(MailConstant.SPAM, receiveMail,idUser,path,contextPath,1);
					}
				}catch(Exception e){
					log.error("邮箱帐号【"+objMap.get("email")+"】同步收件箱，垃圾箱失败，原因是："+e.getMessage(), e);
				}
			}
		} catch (Exception e) {
			log.error("查询账户失败，原因是：" + e.getMessage(), e);
		}
	}
}
